package cst.a526whx.sanjixiangmu_526whx;

import android.widget.Toast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by 王红鑫 on 2017/5/25.
 */

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button btn_login;
    private Button btn_reg;
    private ResModel<AmicoolBean> model;
    private AmicoolListener<LoginBean> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = (EditText)findViewById(R.id.reg_username);
        password = (EditText)findViewById(R.id.reg_password);
        btn_login = (Button)findViewById(R.id.btn_login);
        btn_reg = (Button)findViewById(R.id.btn_reg);
        listener = new AmicoolListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                if(l.getUsername()!=null){
                    String sessionid = l.getSessionid();
                    //把sessionid存到一个地方
                    Intent intent = new Intent(
                            LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onReponse(LoginBean l) {

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this,msg,
                        Toast.LENGTH_SHORT).show();
            }
        };

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model = new ResModel<AmicoolBean>("gson") {
                    @Override
                    public void reg(String s, String s1, String s2, String rr, String s3, String s4, AmicoolListener<LoginBean> listener) {

                    }

                    @Override
                    public void Login(String u, String p, AmicoolListener l) {

                    }

                    @Override
                    public void reg(String u, String p, String t, int roleid, String email) {

                    }

                    @Override
                    public void getList(String mod, int page, String s, AmicoolListener l) {

                    }

                    @Override
                    public void getVideotDetail(String mod, int id, String s, AmicoolListener l) {

                    }
                };
                model.login(username.getText().toString(),
                        password.getText().toString(),
                        listener);
            }
        });
    }
}
